package TCP;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

//客户端发送文件
public class ClientTp {
    public static void main(String[] args) {
        InputStream is=null;
        Socket socket=null;
        OutputStream os=null;
        try {
            InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
            int port=9988;
            socket=new Socket(inetAddress,port);
            os=socket.getOutputStream();
            is=new FileInputStream("Libs/a.jpg");
            int len;
            while ((len = is.read()) != -1) {
                os.write(len);
                //socket.shutdownOutput();表示客户端已经传输完了
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
